package com.le.tester.nioplan.client;

import com.le.tester.nioplan.server.NioService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * createTime：2021/12/1 13:45
 * description：nio客户端
 */
public class NioClient {

    private static final Logger LOG = LoggerFactory.getLogger(NioClient.class);

    //启动
    public void start() throws IOException {
        //连接到服务器
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 9400));
        LOG.info("nio 客户端连接成功！");
        //向服务器发送数据request
        //获取键盘的输入
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            StringBuilder request = new StringBuilder(scanner.nextLine());
            if (StringUtils.isNotBlank(request)) {
                socketChannel.write(StandardCharsets.UTF_8.encode(request.toString()));
            }
        }
        //接收服务器的数据response

    }

    public static void main(String[] args) throws IOException {
        new NioClient().start();
    }
}
